const dbPool = require("../database")

// 获取商品列表路由回调
exports.getList = function (req, res) {
  req.query.page = (req.query.page - 1) * 20;
  let sqlStr = "select * from goods where is_delete = 0 order by id asc limit ?, 20";
  dbPool.query(sqlStr, req.query.page, (err, results) => {
    if (err) return res.cc(err);
    const data = results;
    sqlStr = "select count(*) as len from goods";
    dbPool.query(sqlStr, (err, results) => {
      if (err) return res.cc(err);
      const maxPage = Math.ceil(results[0].len / 20);
      res.send({
        status: 1,
        message: "获取商品信息成功！",
        maxPage,
        data,
      });
    });
  })
}

// 通过名称获取商品列表路由回调
exports.getByName = function (req, res) {
  req.query.name = "%" + req.query.name + "%";
  req.query.page = (req.query.page - 1) * 20;
  let sqlStr = "select * from goods where is_delete = 0 and name like ? order by id asc limit ?, 20";
  dbPool.query(sqlStr, [req.query.name, req.query.page], (err, results) => {
    if (err) return res.cc(err);
    sqlStr = "select count(*) as len from goods where name like ?";
    const data = results;
    dbPool.query(sqlStr, req.query.name, (err, results) => {
      if (err) return res.cc(err);
      const maxPage = Math.ceil(results[0].len / 20);
      res.send({
        status: 1,
        maxPage,
        message: "获取商品信息成功！",
        data,
      });
    });
  });
}

// 通过分类名获取商品列表id路由回调
exports.getByType = function (req, res) {
  req.query.page = (req.query.page - 1) * 20;
  let sqlStr = "select * from goods where is_delete = 0 and type_id = ? order by id asc limit ?, 20";
  dbPool.query(sqlStr, [req.query.type_id, req.query.page], (err, results) => {
    if (err) return res.cc(err);
    const data = results;
    sqlStr = "select count(*) as len from goods where type_id = ?";
    dbPool.query(sqlStr, req.query.type_id, (err, results) => {
      if (err) return res.cc(err);
      const maxPage = Math.ceil(results[0].len / 20);
      res.send({
        status: 1,
        maxPage,
        message: "获取商品信息成功！",
        data,
      });
    });
  });
}

// 通过商品 ID 获取商品详情
exports.getById = function (req, res) {
  const sqlStr = "select goods.*, type.name as type_name from goods join type on goods.type_id = type.id  where goods.is_delete = 0 and goods.id = ?";
  dbPool.query(sqlStr, req.query.id, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 1,
      message: "获取商品详情成功",
      data: results,
    })
  })
}

// 获取新品
exports.getByTime = function (req, res) {
  const sqlStr = "select * from goods where is_delete = 0 order by time desc limit 1, 20";
  dbPool.query(sqlStr, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 1,
      maxPage: 1,
      message: "获取商品信息成功！",
      data: results,
    })
  })
}

// 获取热销商品
exports.getByHot = function (req, res) {
  const sqlStr = "select * from goods where is_delete = 0 order by sales desc, time desc limit 1, 20";
  dbPool.query(sqlStr, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 1,
      maxPage: 1,
      message: "获取商品信息成功！",
      data: results,
    })
  })
}